/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun;

import com.norbl.runjrun.exception.*;


/** <tt>TaskDescription</tt> contains all the information needed
 *  to execute a <tt>Runnable</tt> on a {@link RunnerNode}.
 *  When a <tt>Runnable</tt> is submitted for exceution, the information
 *  in this class is put into an SQS message.
 *
 * @author Barnet Wagman
 */
public class TaskDescription extends AbstractParams {

    public String inputBucketName;
    public String inputS3Key;
    public String outputBucketName;
    public String outputS3Key;    
    public boolean deleteRawAfterRunning;
    public boolean saveCookedRunnable;
    
    public TaskDescription( String inputBucketName,
                            String inputS3Key,
                            String outputBucketName,
                            String outputS3Key,                                                
                            boolean deleteRawAfterRunning,
                            boolean saveCookedRunnable) {
                
        this.inputBucketName = inputBucketName;
        this.inputS3Key = inputS3Key;
        this.outputBucketName = outputBucketName;
        this.outputS3Key = outputS3Key;       
        this.deleteRawAfterRunning = deleteRawAfterRunning;
        this.saveCookedRunnable = saveCookedRunnable;
        
        checkForRequiredParams();
    }

    public TaskDescription(String bucketName,
                           String inputS3Key,String outputS3Key) {
        this(bucketName,inputS3Key,bucketName,outputS3Key,
             true,true);
    }

        /** Creates a <tt>TaskDescription</tt> from a string
         *  created by {@link com.norbl.util.PropertyList#toTextForm()  }.
         *  If the
         *  string does not contain values for all fields, missing
         *  <tt>String</tt> fields are set to <tt>null</tt>, and missing
         *  <tt>boolean</tt> are set to <tt>false</tt>.
         *
         * @param message
         */
    public TaskDescription(String message) {
        setParams(message,this.getClass().getDeclaredFields(),false);
    }

    public TaskDescription() {}

    public String toMessageString() {
        return( toMessageString(TaskDescription.class.getDeclaredFields()) );
    }

    public String toString() { return(toMessageString()); }

        // ----------------------------------------------

    protected void checkForRequiredParams() {

        String m = "";
        if ( inputBucketName == null ) m += "inputBucketName ";
        if ( inputS3Key == null ) m += "inputS3Key ";        
        if ( saveCookedRunnable ) {
            if (outputBucketName == null) m += "outputBucketName ";
            if (outputS3Key == null) m += "outputS3Key ";
        }

        m = m.trim();
        if ( m.length() >= 1 ) {
            throw new MissingParameterException("The following required " +
                "parameters are missing: " + m);
        }
    }
}
